package com.xw.bus;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.text.format.Time;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

public class BusShow extends Activity {
	
	TextView mPosShow;
	char bus114[]={'A','H','K','M','P','T','X'};
	char bus78[]={'B','C','H','L','M','O','Z'};
	Time Time114[]=new Time[7];
	Time Time78[]=new Time[7];
	View mBusMes1;
	char curPos,endPos;
	int ind114,ind78;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.bus_show);
		initTime();
		mPosShow=(TextView)findViewById(R.id.textView1);
		mPosShow.setText("My Position:");
		mBusMes1=findViewById(R.id.busMes1);
		mBusMes1.setVisibility(View.GONE);
		
		mPosShow.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				
				LayoutInflater inflat=LayoutInflater.from(BusShow.this);
				final View myView=inflat.inflate(R.layout.diag_double, null);

				new  AlertDialog.Builder(BusShow.this)  
				.setTitle("Please input position:" )  
				.setIcon(android.R.drawable.ic_dialog_info)  
				.setView(myView)  
				.setPositiveButton("Enter",new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						EditText startView=(EditText)myView.findViewById(R.id.editText1);
						EditText endView=(EditText)myView.findViewById(R.id.editText2);
						curPos=startView.getText().toString().charAt(0);
						endPos=endView.getText().toString().charAt(0);
						getBusMes();
					}
				})  
				.setNegativeButton("Cancel" ,  null )  
				.show();
			}
		});
	}
	public void getBusMes()
	{
		mPosShow.setText("My Position: "+curPos);
		mBusMes1.setVisibility(View.GONE);
		TextView bus_num;
		TextView near_pos;
		TextView near_time;
		int mCurInd=-1,mPurInd=-1;
		for(int i=0;i<bus114.length;i++)
		{
			if(bus114[i]==curPos)
			{
				mCurInd=i;
				ind114=i;
			}
			if(bus114[i]==endPos)
			{
				mPurInd=i;
			}
		}
		if(mCurInd!=-1&&mPurInd!=-1)
		{
			bus_num=(TextView)findViewById(R.id.textView2);
			near_pos=(TextView)findViewById(R.id.textView3);
			near_time=(TextView)findViewById(R.id.textView4);
			if(mCurInd<mPurInd)
				bus_num.setText("114 ("+bus114[0]+"-->"+bus114[bus114.length-1]+")");
			else
				bus_num.setText("114 ("+bus114[bus114.length-1]+"-->"+bus114[0]+")");
			near_pos.setText(""+curPos);
			near_time.setText(Time114[mCurInd].toString());
			mBusMes1.setVisibility(View.VISIBLE);
		}
	}
	public void initTime()
	{
		for(int i=0;i<Time114.length;i++)
		{
			Time114[i]=new Time();
			Time114[i].set(2, 9, 2012);
			Time78[i]=new Time();
			Time78[i].set(2,9,2012);
		}
	}
	public String getPastTime(Time t)
	{
		String res=new String();
		res="No Data";
		Time oldT=new Time();
		oldT.set(2,9,2012);
		if(Time.compare(t,oldT)!=0)
		{
			Time curT=new Time();
			curT.setToNow();
			if(t.year==curT.year
			   && t.month==curT.month
			   && t.monthDay==curT.monthDay)
			{
				int curS=curT.second;
				int curM=curT.minute;
				int curH=curT.hour;
				int mCurSum=(curH*60+curM)*60+curS;
				int tSum=(t.hour*60+t.minute)*60+t.second-mCurSum;
				
				int resH=tSum/3600;
				int resM=tSum%3600/60;
				int resS=tSum%3600%60;
				res=String.valueOf(resH)+" Hour "+
					String.valueOf(resM)+" Min "+
					String.valueOf(resS)+" Sec ";
			}
		}
		return res;
	}
}
